package com.qch.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author qch
 * @Date 2021/5/17
 */
public class MyConsumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");
        Connection connection = connectionFactory.newConnection();
        final Channel channel = connection.createChannel();
        channel.queueDeclare("queue.ca", false, false, false, null);
        //ackAck:false手动确认消息
        channel.basicConsume("queue.ca",false,"myConsumer",new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                //确认消息
               // channel.basicAck(envelope.getDeliveryTag(),false);
                //1.消息的标签
                //2.表示不确认多个消息还是一个消息
                //3.不确认的消息是否需要重新入列重发
                //用于拒收多条消息
                //channel.basicNack(envelope.getDeliveryTag(),false,true);
                //用于拒收1条消息
                //重新入列重发
                channel.basicReject(envelope.getDeliveryTag(),true);
            }
        });
    }
}
